package com.hxgis.oc.product.base;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class ProductFactoryManager {
    private static final Map<String,ProductFactory> PRODUCT_FACTORY_LIST = new HashMap<>();

    public static Collection<ProductFactory> getFactoryCollection(){
        return PRODUCT_FACTORY_LIST.values();
    }

    public static ProductFactory getFactory(String name){
        return PRODUCT_FACTORY_LIST.get(name);
    }

    public static void add(String name,ProductFactory listener)
    {
        PRODUCT_FACTORY_LIST.put(name,listener);
    }

    public static void publish(ProduceTask o)
    {
        PRODUCT_FACTORY_LIST.forEach((k,listener)->listener.onPublish(o));
    }

    public static void retrieve(ProduceTask o)
    {
        PRODUCT_FACTORY_LIST.forEach((k,listener)->listener.onRetrieve(o));
    }

    private static final HashSet<String> registerNames = new HashSet<>();

    public static void register(String dataName){
        if(registerNames.contains(dataName)){
            return;
        }
        registerNames.add(dataName);

        Collection<ProductFactory> factoryList = ProductFactoryManager.getFactoryCollection();
        for(ProductFactory factory : factoryList){
            factory.registerProduct(dataName);
        }
    }
}
